<?php
/**
 * User: krisbuist
 * Date: 1/31/12
 * Time: 2:07 PM
 */

require_once APPPATH . 'core/controller.php';
require_once APPPATH . 'libraries/generators/csvReader.php';
require_once APPPATH . 'libraries/generators/nameGenerator.php';

class UserGenerator extends Controller
{
    protected $account_types = array();
    protected $step = 5;

    public function __construct()
    {
        parent::__construct();
        //$this->load->library('generators/csvReader');
        //$this->load->library('generators/nameGenerator');


        $this->account_types[] = $this->em->find('Account_Type', 1);
        $this->account_types[] = $this->em->find('Account_Type', 2);
    }

    public function generateUsers()
    {
        $nameGen = new NameGenerator();
        for ($i = 0; $i < $this->step; $i++)
        {
            try
            {
                $user = new User();

                $firstName = $nameGen->randomElement('firstName');
                $lastName = $nameGen->randomElement('lastName');
                $name = $firstName . ' ' . $lastName;
                $user->setName($name);

                $user->setEmail(str_replace(" ", "", $name) . '@nieuwsbuis');

                $genders = array('m', 'v');
                $user->setGender($genders[rand(0, 1)]);

                $user->setPassword(sha1($firstName));

                $user->setUsername($firstName . '.' . $lastName);

                $accountTypeIndex = rand(0, 1);
                $user->setType($this->account_types[$accountTypeIndex]);

                if ($accountTypeIndex == 1)
                {
                    $user->setBankAccount("1234567890");
                }

                $this->em->persist($user);
            }
            catch (PDOException $e)
            {
                echo $e;
            }
        }

        try
        {
            $this->em->flush();
        }
        catch (PDOException $e)
        {
            echo $e;
        }

        $numberOfUsers = $this->em->createQuery("SELECT COUNT(u) FROM User u")->getSingleScalarResult();
        if($numberOfUsers >= 10000)
        {
            echo 'Finished!';
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0;url=/generators/userGenerator/generateUsers">';
            echo '<title>' . $numberOfUsers . '</title>';
        }
    }
}
